﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using System.Data;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.SaaS.OrderManage
{
    public partial class EmptyOrderPending : BasePage
    {
        public override string FunctionID
        {
            get
            {
                return "Func3011";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtOrderDateBegin.Attributes.Add("onclick", "WdatePicker();");
            txtOrderDateEnd.Attributes.Add("onclick", "WdatePicker();");
            if (!IsPostBack)
            {
                MasterDataBll bll = new MasterDataBll();
                List<BasProductCateEntity> list = bll.GetAll<BasProductCateEntity>(" and Grade=2");
                ddlCate.DataSource = list;
                ddlCate.DataTextField = "CateName";
                ddlCate.DataValueField = "CateCode";
                ddlCate.DataBind();
                ddlCate.Items.Insert(0, new ListItem("全部", ""));

                //ddlOrderStatus.BindEnum(EnumDescAttribute.GetFieldTexts(typeof(EnumHelper.OrderStatus)));
                //ddlOrderStatus.Items.Insert(0, new ListItem("全部", ""));
                ddlOrderEmptyStatus.BindEnum(EnumDescAttribute.GetFieldTexts(typeof(EnumHelper.OrderEmptyStatus)));
                ddlOrderEmptyStatus.Items.Insert(0, new ListItem("全部", ""));

                BindGrid(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(Page_Load);
            AspNetPager1.PageChanged += new EventHandler(AspNetPager1_PageChanged);
            lnkbtnQuery.Click += new EventHandler(lnkbtnQuery_Click);
            gvOrderList.RowDataBound += new GridViewRowEventHandler(gvOrderList_RowDataBound);

            lnkbtnExport.Click += new EventHandler(lnkbtnExport_Click);
        }
        void lnkbtnExport_Click(object sender, EventArgs e)
        {
            ExportGrid();
        }

        void gvOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblStatus = e.Row.FindControl("lblStatus") as Label;
                DataRowView dv = e.Row.DataItem as DataRowView;
                int OrderStatus = Convert.ToInt32(dv["OrderStatus"].ToString());
                lblStatus.Text = EnumDescAttribute.GetFieldText((EnumHelper.OrderStatus)OrderStatus);

                Literal ltlType = e.Row.FindControl("ltlType") as Literal;
                int OrderType = Convert.ToInt32(dv["OrderType"].ToString());
                ltlType.Text = EnumDescAttribute.GetFieldText((EnumHelper.OrderType)OrderType);

                Label lblEmptyStatus = e.Row.FindControl("lblEmptyStatus") as Label;
                int OrderEmptyStatus = Convert.ToInt32(dv["OrderEmptyStatus"].ToString());
                lblEmptyStatus.Text = EnumDescAttribute.GetFieldText((EnumHelper.OrderEmptyStatus)OrderEmptyStatus);

                if(OrderEmptyStatus == (int)EnumHelper.OrderEmptyStatus.Complete)
                {
                    HyperLink lnkEdit = e.Row.FindControl("lnkEdit") as HyperLink;
                    lnkEdit.Visible = false;
                }
            }
        }

        void lnkbtnQuery_Click(object sender, EventArgs e)
        {
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
        }

        void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        private void BindGrid(int PageIndex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;

            if (!string.IsNullOrEmpty(txtOrderDateBegin.Text))
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[OrderDate]";
                parm.ColumnType = DbType.String;
                parm.ParameterName = "@OrderDateBegin";
                parm.ParameterValue = Convert.ToDateTime(txtOrderDateBegin.Text);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
            }

            if (!string.IsNullOrEmpty(txtOrderDateEnd.Text))
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[OrderDate]";
                parm.ColumnType = DbType.String;
                parm.ParameterName = "@OrderDateEnd";
                parm.ParameterValue = Convert.ToDateTime(txtOrderDateEnd.Text).AddDays(1);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Less;
                parms.Add(parm);
            }
            if (!string.IsNullOrEmpty(ddlOrderEmptyStatus.SelectedValue))
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[OrderEmptyStatus]";
                parm.ColumnType = DbType.Int32;
                parm.ParameterName = "@OrderEmptyStatus";
                parm.ParameterValue = Convert.ToInt32(ddlOrderEmptyStatus.SelectedValue);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
            }

            parm = new SqlDbParameter();
            parm.ColumnName = "[OrderType]";
            parm.ColumnType = DbType.Int32;
            parm.ParameterName = "@OrderType";
            parm.ParameterValue = (int)EnumHelper.OrderType.EmptyOrder;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            int RecordCount = 0;
            DataTable dt = new OrderBll().QueryOrderList(parms, txtProductName.Text, ddlCate.SelectedValue, "OrderDate DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvOrderList.DataSource = dt;
            gvOrderList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        private void ExportGrid()
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;

            if (!string.IsNullOrEmpty(txtOrderDateBegin.Text))
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[OrderDate]";
                parm.ColumnType = DbType.String;
                parm.ParameterName = "@OrderDateBegin";
                parm.ParameterValue = Convert.ToDateTime(txtOrderDateBegin.Text);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.GreaterAndEqual;
                parms.Add(parm);
            }

            if (!string.IsNullOrEmpty(txtOrderDateEnd.Text))
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[OrderDate]";
                parm.ColumnType = DbType.String;
                parm.ParameterName = "@OrderDateEnd";
                parm.ParameterValue = Convert.ToDateTime(txtOrderDateEnd.Text).AddDays(1);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Less;
                parms.Add(parm);
            }
            if (!string.IsNullOrEmpty(ddlOrderEmptyStatus.SelectedValue))
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[OrderEmptyStatus]";
                parm.ColumnType = DbType.Int32;
                parm.ParameterName = "@OrderEmptyStatus";
                parm.ParameterValue = Convert.ToInt32(ddlOrderEmptyStatus.SelectedValue);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
            }

            parm = new SqlDbParameter();
            parm.ColumnName = "[OrderType]";
            parm.ColumnType = DbType.Int32;
            parm.ParameterName = "@OrderType";
            parm.ParameterValue = (int)EnumHelper.OrderType.EmptyOrder;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            DataTable dt = new OrderBll().ExportOrderList(parms);

            //整理dt
            dt.Columns.Add("订单状态");
            foreach (DataRow row in dt.Rows)
            {
                EnumHelper.OrderStatus status = (EnumHelper.OrderStatus)Convert.ToInt32(row["OrderStatus"].ToString());
                row["订单状态"] = EnumDescAttribute.GetFieldText(status);
            }

            dt.Columns["EntName"].ColumnName = "客户名";
            dt.Columns["CustomerName"].ColumnName = "用户名";
            dt.Columns["OrderCode"].ColumnName = "订单号";
            dt.Columns["OrderDate"].ColumnName = "订单时间";
            //dt.Columns["OrderStatus"].ColumnName = "订单状态";
            dt.Columns["ProductCode"].ColumnName = "产品编号";
            dt.Columns["ProductName"].ColumnName = "产品名称";
            dt.Columns["Alias"].ColumnName = "简称";
            dt.Columns["Standard"].ColumnName = "标准";
            dt.Columns["Unit"].ColumnName = "单位";
            dt.Columns["Quantity"].ColumnName = "订货数量";
            dt.Columns["ActualQty"].ColumnName = "实际数量";
            dt.Columns["AssignQty"].ColumnName = "配货数量";

            dt.Columns.Remove("OrderStatus");
            dt.Columns.Remove("CustomerID");
            dt.Columns.Remove("OrderID");
            dt.Columns.Remove("OrderType");

            using (ExcelEPPlusEngine engine = new ExcelEPPlusEngine())
            {
                engine.New("订单");
                engine.SetWorkRange("A1");
                engine.LoadFromDataTable(dt, true);

                var dateColumns = from DataColumn d in dt.Columns
                                  where d.DataType == typeof(DateTime) || d.ColumnName.Contains("Date")
                                  select d.Ordinal + 1;

                foreach (var dc in dateColumns)
                {
                    engine.SetWorkRange(2, dc, dt.Rows.Count + 1, dc);
                    engine.GetCurrentWorkRange().Style.Numberformat.Format = "yyyy-MM-dd";
                }

                engine.SaveAs(Guid.NewGuid() + ".xlsx");
            }
        }
    }
}